<template>
  <div>
    <el-col :span="8" style="width: 100%">
      <el-card shadow="hover">
        <el-form :inline="true" class="demo-form-inline">
          <el-form-item label="审批人">
            <el-input v-model="select.id" placeholder="审批人"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="inquireAdd">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleAdd">添加</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </el-col>
    <el-table :data="tableData" style="width: 100%" border>
      <el-table-column label="Id">
        <template slot-scope="scope">
          <i class=""></i>
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户名">
        <template slot-scope="scope">
          <i class=""></i>
          <span>{{ scope.row.username }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否启用">
        <template slot-scope="scope">
          <i class=""></i>
          <span>{{ scope.row.isActived }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间">
        <template slot-scope="scope">
          <i class=""></i>
          <span>{{ scope.row.createdTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注">
        <template slot-scope="scope">
          <i class=""></i>
          <span>{{ scope.row.remarks }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="160px">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger text"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 增加和修改需要使用的状态框 -->
    <el-dialog
      title="用户信息"
      :visible.sync="dialogFormVisible"
      width="30%"
      center
    >
      <el-form :model="fromData">
        <el-form-item label="用户名" label-width="90px">
          <el-input v-model="fromData.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="备注" label-width="90px">
          <el-input v-model="fromData.remarks" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleCancl">取 消</el-button>
        <el-button :plain="true" type="primary" @click="handleSave"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <!-- 分页框 -->
    <div class="block">
      <el-pagination
        :current-page="pager.pageIndex"
        :page-size="pager.pageSize"
        layout="total, prev, pager, next, jumper, sizes"
        :total="pager.rowsTotal"
        @current-change="handlePageIndexChange"
        @size-change="handlePageSizeChange"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import {getList, addUser, modUser, deleteUser } from "../api/users";
export default {
  data() {
    return {
      tableData: [],
      pager: {
        pageIndex: 1,
        pageSize: 10,
        totalRows: 30,
      },
      fromData: {
        id: 0,
        username: "",
        password: "113",
        remarks: "",
      },
      select:{
        id:""
      },
      currentIndex: 0,
      dialogFormVisible: false,
    };
  },
  methods: {
    inquireAdd() {
      console.log(this.select.id);
      if (this.select.id != "" && this.select.id==Int16Array) {
        getList(this.select.id)
          .then((res) => {
            console.log(res);
            if (res.data != null) {
              this.tableData = [];
              this.tableData.push(res.data);
              // console.log(this.tableData);
            } else {
              console.log("暂时没有Id");
            }
          })
          .catch((err) => {
            console.log(err);
          });
      } else {
        this.fetchData(this.pager);
      }
    },
    handleAdd() {
      this.dialogFormVisible = true;
      this.fromData.id = 0;
      this.fromData.username = "";
      this.fromData.remarks = "";
      this.fetchData(this.pager);
    },
    handleEdit(index, row) {
      this.dialogFormVisible = true;
      this.fromData.id = row.id;
      this.fromData.username = row.username;
      this.fromData.remarks = row.remarks;
      this.currentIndex = index;
    },
    handleDelete(index, row) {
      this.fromData.id = row.id;
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteUser(this.fromData.id).then(({ data }) => {
            console.log(data);
            this.tableData.splice(index, 1);
            this.fetchData(this.pager);
            if (index == 0) {
              let newIndex = this.pager.pageIndex - 1;
              this.handlePageIndexChange(newIndex);
              this.fetchData(this.pager);
            }
          });

          this.$message(
            {
              type: "success",
              message: "删除成功!",
              rest: 0,
            },
            {}
          );
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
      // console.log(index, row);
      // this.fromData.id = row.id;
      // console.log(this.fromData);
      // deleteUser(this.fromData.id).then(({ data }) => {
      //   console.log(data);
      //   this.fetchData(this.pager);
      // });
    },
    handleSave() {
      this.dialogFormVisible = false;
      this.$message({
        message: "恭喜你，你成功了成功消息",
        type: "success",
      });
      console.log(this.fromData);
      if (this.fromData.id === 0) {
        addUser(this.fromData).then((res) => {
          this.tableData.push(res.data);
        });
      } else {
        modUser(this.fromData.id, this.fromData).then((res) => {
          this.tableData.splice(this.currentIndex, 1, res.data);
        });
      }
    },
    handleCancl() {
      this.dialogFormVisible = false;
    },
    handlePageIndexChange(val) {
      this.pager.pageIndex = val;
      this.fetchData(this.pager);
    },
    handlePageSizeChange(val) {
      this.pager.pageSize = val;
      this.fetchData(this.pager);
    },
    fetchData(pager) {
      getList(pager)
        .then((res) => {
          this.tableData = res.data.data;
          this.pager = res.data.pager;
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
  mounted() {
    // getById(1).then(({data})=>{
    //   console.log(data);
    // }).catch(err=>{
    //   console.log(err);
    // })
    this.fetchData(this.pager);
  },
};
</script>